﻿#include <QCoreApplication>

#include "command.h"
#include <QString>
#include "string.h"
#include "filecommon.h"
#include "httphelper.h"
#include "compresshelper.h"
#include <QDebug>
#include <QDir>
#include <QTextCodec>
#include <QHostInfo>


const QString HOST = "http://update.gonsincloud.com/en";
const QString GIT_URL = HOST + "/git.exe";
const QString PYTHON_URL = HOST + "/python-3.11.0-embed-amd64.zip";
const QString WGET = HOST + "/wget.exe";
const QString EN_CACHE_PATH = "C:/.en_cache";

const QString GIT_HOME = EN_CACHE_PATH + "/git";
const QString PYTHON_HOME = EN_CACHE_PATH + "/python";
const QString EN_REPOSITORY_URL = "https://gitee.com/dwdyoung/g-environment";


#ifdef WIN32
const QString EN_PATH = EN_CACHE_PATH + "/en";
#else
const QString EN_PATH = "/usr/bin/en_git";
#endif



/**
 * 安装git
 */
void installGit(){
#ifdef WIN32
    if(!FileCommon::INSTANCE().isDir(GIT_HOME)){
        HttpHelper::INSTANCE().downloadFileFromURL(GIT_URL, "git.exe");
        FileCommon::INSTANCE().createDir(GIT_HOME);
        FileCommon::INSTANCE().moveFile("git.exe", GIT_HOME + "/git.exe");
    }
#else
    QString p = QSysInfo::productType();
    QString command = "";
    if(p == "ubuntu"){
        command = "apt install git -y";
    } else if(p == "centos") {
        command = "yum install git -y";
    } else {
        command = "apt install git -y";
    }
    Command::INSTANCE().cmd(command);
#endif

}



/**
 * 安装python
 */
void installPython(){
#ifdef WIN32
    if(!FileCommon::INSTANCE().isDir(PYTHON_HOME)){
        HttpHelper::INSTANCE().downloadFileFromURL(PYTHON_URL, "python.zip");
        FileCommon::INSTANCE().createDir(PYTHON_HOME);
        CompressHelper::INSTANCE().decompress("python.zip", PYTHON_HOME);
        FileCommon::INSTANCE().deleteFile("python.zip");
    }
#else
    // 使用apt安装python
    QString p = QSysInfo::productType();
    QString command = "";
    if(p == "ubuntu"){
        command = "apt install python3 -y";
    } else if(p == "centos") {
        command = "yum install python3 -y";
    } else {
        command = "apt install python3 -y";
    }
    Command::INSTANCE().cmd(command);
#endif
}


/**
 * 安装en工具
 * @brief installEn
 */
void installEn(){
    if(!FileCommon::INSTANCE().isDir(EN_PATH)){
        QDir enDir(EN_PATH);
        Command::INSTANCE().cmd("git", new QString[3]{"clone", EN_REPOSITORY_URL, enDir.absolutePath()}, 3);
    }
}



int main(int argc, char *argv[])
{
//    system("chcp  65001");

    qDebug() << "localHOstName: " << QHostInfo::localHostName();
    qDebug() << "localDomainName: " << QHostInfo::localDomainName();
    qDebug() << "WindowsVersion: " << QSysInfo::WindowsVersion;
    qDebug() << "buildAbi: " << QSysInfo::buildAbi();
    qDebug() << "buildCpuArchitecture: " << QSysInfo::buildCpuArchitecture();
    qDebug() << "currentCpuArchitecture: " << QSysInfo::currentCpuArchitecture();
    qDebug() << "kernelType: " << QSysInfo::kernelType();     //内核类型
    qDebug() << "kernelVersion: " << QSysInfo::kernelVersion();    //内核版本
    qDebug() << "machineHostName: " << QSysInfo::machineHostName();   // 主机名称
    qDebug() << "prettyProductName: " << QSysInfo::prettyProductName();   //操作系统的产品名称
    qDebug() << "productType: " << QSysInfo::productType();
    qDebug() << "productVersion: " << QSysInfo::productVersion();  //产品版本

#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
    QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
#else
//    QTextCodec *codec = QTextCodec::codecForName("utf-8");
#ifdef WIN32
    QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
    QTextCodec::setCodecForLocale(codec);
#endif



#ifdef WIN32
    if(!FileCommon::INSTANCE().isDir(EN_CACHE_PATH)){
        FileCommon::INSTANCE().createDir(EN_CACHE_PATH);
    }
#endif

    // 检查python文件夹是否存在
    if(!Command::INSTANCE().isCommandExist("python")){
        qDebug() << "正在安装Python";
        installPython();

#ifdef WIN32
        QString PATH = qgetenv("PATH");
        PATH = PATH + ":" + PYTHON_HOME;
        qputenv("PYTHON_HOME", PYTHON_HOME.toUtf8());
        qputenv("PATH", PATH.toUtf8());
#endif
        qDebug() << "安装Python成功";
    }

    // 检查git工具是否存在
    if(!Command::INSTANCE().isCommandExist("git")){
        qDebug() << "正在安装git";
        installGit();

#ifdef WIN32
        QString PATH = qgetenv("PATH");
        PATH = PATH + ":" + GIT_HOME;
        qputenv("GIT_HOME", GIT_HOME.toUtf8());
        qputenv("PATH", PATH.toUtf8());
#endif

        qDebug() << "安装git成功";
    }


    // 检查python文件夹是否存在
    if(Command::INSTANCE().isCommandExist("en")){
        qDebug() << "你已经安装过en工具了。";
        return 0;
    }

    // 检查en文件夹是否存在
    if(!FileCommon::INSTANCE().isExist(EN_PATH)){
        qDebug() << "正在安装en工具";
        installEn();
        qputenv("EN_PATH", EN_PATH.toUtf8());
        qDebug() << "安装en工具成功";
    }


    // 设置 EN_PATH 环境变量，并且设置PATH环境变量，将en接入到命令行工具中。
#ifdef WIN32
    if(Command::INSTANCE().cmd("setx", new QString[]{"EN_PATH", EN_PATH}, 2)){
        QString PATH = qgetenv("PATH");
        PATH = PATH + ";" + EN_PATH;
        if(Command::INSTANCE().cmd("setx", new QString[]{"PATH", PATH}, 2)){
            qDebug() << "en工具已经安装成功，安装目录为C:/.en_cache";
            qDebug() << "";
            qDebug() << "请删除当前的安装软件，以免造成影响。";
            qDebug() << "你可以使用“en uninstall en”卸载en工具。";
            qDebug() << "更多使用方式可以输入“en --help”查看。";
            qDebug() << "";
            qDebug() << "如果提示找不到en命令，请重新登出再登入。";
            return 0;
        }
    }
#else
    QString shellContent = "cat >> /usr/bin/en <<- 'OOOO'\n \
        EN_PATH=/usr/bin/en_git\n \
        PYTHONDONTWRITEBYTECODE=1 exec python3 \"$EN_PATH/en.py\" \"$@\"\n \
        OOOO\n \
        chmod 777 /usr/bin/en\";\n ";
    FileCommon::INSTANCE().writeFile("update_environment.sh", shellContent);
    Command::INSTANCE().cmd("chmod 777 update_environment.sh");
    if(Command::INSTANCE().cmd("./update_environment.sh")){
        FileCommon::INSTANCE().deleteFile("update_environement.sh");
        qDebug() << "en工具已经安装成功";
        qDebug() << "";
        qDebug() << "请删除当前的安装软件，以免造成影响。";
        qDebug() << "你可以使用“en uninstall en”卸载en工具。";
        qDebug() << "更多使用方式可以输入“en --help”查看。";
        qDebug() << "";
        qDebug() << "如果提示找不到en命令，请重新登出再登入。";
        return 0;
    }

#endif
    qDebug() << "en工具安装失败，请使用管理员权限重新运行。";
    return 1;

//    return a.exec();
}
